<template>
  <div class="admin-login-container">
    <div class="login-form-wrapper">
      <div class="login-header">
        <h2>校园外卖平台管理系统</h2>
      </div>
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="el-icon-user" 
            placeholder="请输入管理员账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="el-icon-lock" 
            type="password" 
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            class="login-button" 
            @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { updateLastLoginTime } from '@/api/user'

export default {
  name: 'AdminLogin',
  data() {
    return {
      // 登录表单
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
        ]
      },
      // 加载状态
      loading: false
    }
  },
  methods: {
    // 处理登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 调用登录接口
          login(this.loginForm)
            .then(response => {
              // 检查是否是管理员角色
              if (response.data.data.user.role === 3) {
                // 更新最后登录时间
                updateLastLoginTime(response.data.data.user.id)
                  .then(() => {
                    this.$message.success('登录成功')
                    this.$store.commit('SET_TOKEN', response.data.data.token)
                    this.$store.commit('SET_USER', response.data.data.user)
                    // 跳转到管理员首页
                    this.$router.push('/admin/dashboard')
                  })
                  .catch(error => {
                    console.error('更新登录时间失败', error)
                  })
              } else {
                this.$message.error('您不是管理员，无法登录管理系统')
                // 清除登录信息
                this.$store.commit('LOGOUT')
              }
            })
            .catch(error => {
              console.error('登录失败', error)
              this.$message.error('登录失败，请检查账号和密码')
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(to bottom right, #1890ff, #36cfc9);
}

.login-form-wrapper {
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}

.login-button {
  width: 100%;
}
</style>